package cn.yr.clock.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.yr.clock.domain.entity.Prize;
import cn.yr.clock.domain.entity.PrizeInstance;
import cn.yr.clock.domain.vo.PageVo;
import cn.yr.clock.domain.vo.PrizeInstanceVo;
import cn.yr.clock.handler.CacheUserHandler;
import cn.yr.clock.service.PrizeInstanceService;
import cn.yr.clock.service.PrizeService;
import cn.yr.common.exception.ret.Ret;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 
 * @author Lang
 * @email 1102076808@qq.com
 * @date 2021-09-25 11:13:41
 */
@RestController
@RequestMapping("/prizeInstance")
public class PrizeInstanceController{
    @Autowired
    private PrizeInstanceService prizeInstanceService;
    @Autowired
    private CacheUserHandler cacheUserHandler;
    @Autowired
    private PrizeService prizeService;

    @RequestMapping("/generate")
    public Ret generatePrizeInstance(Long prizeId,Integer generateNum){
        return prizeInstanceService.generate(prizeId,generateNum);
    }

    @RequestMapping("/listPrize")
    public Ret listPrize(){
        Long userId = cacheUserHandler.getUser().getUserId();
        List<PrizeInstanceVo> prizeInstanceVos = prizeInstanceService.listPrize(userId);
        return Ret.success(prizeInstanceVos);
    }

    /**
     * 劵码核销
     * @return
     */
    @RequestMapping("/verifyPrize")
    public Ret verifyPrize(@RequestBody String prizeCode){
        return prizeInstanceService.verifyPrize(prizeCode);
    }

    @RequestMapping("/getPrizeVoByPrizeCode")
    public Ret getPrizeVoByPrizeCode(@RequestBody String prizeCode){
        QueryWrapper<PrizeInstance> wrapper = new QueryWrapper<>();
        wrapper.eq("prize_code",prizeCode);
        PrizeInstance prizeInstance = prizeInstanceService.getOne(wrapper);
        if(ObjectUtil.isNull(prizeInstance)){
            return Ret.error("未找到该奖券");
        }
        Prize prize = prizeService.getById(prizeInstance.getPrizeId());
        PrizeInstanceVo prizeInstanceVo = new PrizeInstanceVo();
        BeanUtil.copyProperties(prizeInstance,prizeInstanceVo);
        BeanUtil.copyProperties(prize,prizeInstanceVo);
        return Ret.success(prizeInstanceVo);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Ret list(@RequestBody PageVo<PrizeInstance> pageVo){
    	PageVo<PrizeInstance> pagePrizeInstance = prizeInstanceService.page(pageVo,new QueryWrapper<>());
        return Ret.success(pagePrizeInstance);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{prizeInstanceId}")
    public Ret info(@PathVariable("prizeInstanceId") Long prizeInstanceId){
		PrizeInstance  prizeInstance = prizeInstanceService.getById(prizeInstanceId);
	    return Ret.success(prizeInstance);
    }

    /**
     * 保存
     */
    @RequestMapping("/add")
    public Ret save(@Validated @RequestBody PrizeInstance prizeInstance){
		prizeInstanceService.save(prizeInstance);
	    return Ret.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    public Ret update(@Validated @RequestBody PrizeInstance prizeInstance){
		prizeInstanceService.updateById(prizeInstance);
	    return Ret.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/remove")
    public Ret delete(@RequestBody Long[] prizeInstanceIds){
		prizeInstanceService.removeByIds(Arrays.asList(prizeInstanceIds));
        return Ret.success();
    }

}
